package cn.bobohost.crmrbac.service.impl;

import cn.bobohost.crmrbac.dao.mapper.DepartmentMapper;
import cn.bobohost.crmrbac.pojo.po.Department;
import cn.bobohost.crmrbac.pojo.query.QueryObject;
import cn.bobohost.crmrbac.service.DepartmentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//@RequiredArgsConstructor
//@Transactional
@Transactional(readOnly = true) //默认只读事务，//所有的方法都有效，当然方法上可以覆盖
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper,Department> implements DepartmentService {
    //注入Mapper
//    @Autowired
//    private DepartmentMapper departmentMapper;

//    @Autowired
//    public DepartmentServiceImpl(DepartmentMapper departmentMapper) {
//        this.departmentMapper = departmentMapper;
//    }

    @Override
//    @Transactional(readOnly = true) //只读事务，可以提升性能
    public List<Department> findDepartmentList() {
//        return departmentMapper.selectList(null);
        return baseMapper.selectList(null);

    }

    @Override
    @Transactional  ////方法中如果有多个增删改，一定要加；建议增删改都加
    public void saveDepartment(Department department) {

        baseMapper.insert(department);
    }

    @Override
    @Transactional  //事务必须是可读写的
    public void saveOrUpdateDepartment(Department department) {
        //手动写法：先获取部门id，判断id是否存在，如果不存在，则保存，如果存在则更新
        //封装写法：直接使用mybatisplus的方法,底层帮你判断了
        baseMapper.insertOrUpdate(department);

    }

    @Override
    public IPage<Department> findDepartmentListPage(IPage page) {


        //方案1：使用mybatis-plus自带的分页功能，进行带分页的部门列表查询
        IPage<Department> pageResult = baseMapper.selectPage(page, null);

        return pageResult;
    }

    @Override
    public PageInfo<Department> findDepartmentListPageByPageHelper(QueryObject queryObject) {

        //添加一个PageHelper的操作
        //参数1：当前页面，参数2：每页最大可显示的记录数
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());

        //正常查询部门列表(不带分页)
        List<Department> departmentList = baseMapper.selectList(null);

        //将数据列表封装到分页信息结果中
        PageInfo<Department> pageInfo = new PageInfo<>(departmentList);

        return pageInfo;
    }


}
